package objetos3.tp4.metaprogramacion;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class AutoDelegateInvocationHandler<T> implements InvocationHandler {
	
	private T original;
	private Object interceptor;
	
	public AutoDelegateInvocationHandler(T original, Object interceptor) {
		this.original = original;
		this.interceptor = interceptor;
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			Method[] methods = this.getInterceptor().getClass().getMethods();
			for (int i = 0; i < methods.length; i++) {
				if (methods[i].getName().equals(method.getName()))
					return methods[i].invoke(this.getInterceptor(), this.getOriginal(), args[0]);
			}
			return method.invoke(this.getOriginal(), args);
	}
	
	
	
	
	

	public T getOriginal() {
		return original;
	}

	public Object getInterceptor() {
		return interceptor;
	}

}
